<template>
  <div class="settings-page">
    <div class="container page">
      <div class="row">
        <div class="col-md-6 offset-md-3 col-xs-12">
          <h1 class="text-xs-center">Your Settings</h1>
          <form @submit="handleUpdateUser">
            <fieldset>
              <fieldset class="form-group">
                <input v-model="profile.image" class="form-control" type="text" :placeholder="user.image" />
              </fieldset>
              <fieldset class="form-group">
                <input
                  v-model="profile.username"
                  class="form-control form-control-lg"
                  type="text"
                  :placeholder="user.username"
                />
              </fieldset>
              <fieldset class="form-group">
                <textarea
                  v-model="profile.bio"
                  class="form-control form-control-lg"
                  rows="8"
                  :placeholder="user.bio"
                ></textarea>
              </fieldset>
              <fieldset class="form-group">
                <input
                  v-model="profile.email"
                  class="form-control form-control-lg"
                  type="text"
                  :placeholder="user.email"
                />
              </fieldset>
              <fieldset class="form-group">
                <input
                  v-model="profile.password"
                  class="form-control form-control-lg"
                  type="password"
                  :placeholder="user.password"
                />
              </fieldset>
              <button class="btn btn-lg btn-primary pull-xs-right" type="button" @click="handleUpdateUser">
                Update Settings
              </button>
            </fieldset>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { updateUser } from '@/api/user';
import { mapState } from 'vuex';

export default {
  middleware: 'authenticated',
  name: 'SettingIndex',
  data() {
    return {
      profile: {
        image: '',
        username: '',
        password: '',
        email: '',
        bio: '',
      },
    };
  },
  mounted() {
    let user = this.user;
    let profile = {
      username: user.username,
      email: user.email,
      bio: user.bio,
      image: user.image,
      password: user.password,
    };
    this.profile = profile;
  },
  methods: {
    async handleUpdateUser() {
      let profile = this.profile;
      const data = await updateUser(profile);
      console.log('handleUpdateUser -> data', data);
    },
  },
  computed: {
    ...mapState(['user']),
  },
};
</script>

<style></style>
